// pages/orders/technicianEvaluate/technicianEvaluate.js
import api from '../../../utils/util.js'; // 导入请求类
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navmess:{
      navtitle: "评价",
      backhide: 0,
      topheight: 1
    },
    text: '', //评价的内容
    sign: '', // 美容师的签名
    token: wx.getStorageSync('token'), // 获取token
    EvaluateList: [], // 评价列表数据 
    scroll_view: '',// 评论列表滑动高度
  },
  /**
   * 参数：sign 
   * /mini/appoint/staffEvaluateList 评价列表接口  /mini/appoint/evaluateStaff 评价
   * 
   * 
  */
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    this.setData({sign: options.sign});
    let query = wx.createSelectorQuery();
    query.select('#navbar').boundingClientRect()
    query.select('.top-evaluate').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function(res){
      let barHeight = res[0].height, top_evaluateHeight = res[1].height, scroll_view = '';
      scroll_view = (res[2].scrollHeight - (barHeight + top_evaluateHeight))+'px';
      self.setData({scroll_view: scroll_view})
      console.log(res[2].scrollHeight);
    })
    this.getUserListText(this.data.sign);
  },
  // 获取用户输入的评价的内容
  getText: function(e){
    this.setData({text: e.detail.value});
  },
  // 用户点击评论
  pushUserEvaluate: function(){
    let comment = this.data.text;
    if(comment.length == 0){this.toast('评论失败，请稍后再试'); return;};
    let sign = this.data.sign, token = this.data.token;
    api.post('/mini/appoint/evaluateStaff',{sign: sign, token: token, comment: comment}).then((res)=>{
      if(res.result!=true){this.toast('评论失败，请稍后再试'); return;}
      this.toast('评论成功');
      this.setData({text: ''}); // 清除评价框内容
      this.getUserListText(this.data.sign); // 评价成功重新请求数据
    })
    
  },
  // 获取评价列表
  getUserListText: function(sign){
    api.post('/mini/appoint/staffEvaluateList',{sign: sign}).then((res)=>{
      if(res.code != 200){this.toast('评价数据拉取失败'); return;}
      let resdata = JSON.parse(JSON.stringify(res.result.list));
      for(var key in resdata){
        resdata[key].photoUrl = resdata[key].photoUrl==undefined?"":resdata[key].photoUrl;
        resdata[key].nick = resdata[key].nick==undefined?"":resdata[key].nick.substr(0,1)+"***";
        resdata[key].createTime = resdata[key].createTime==undefined?"":this.getLocalTime(resdata[key].createTime);
        resdata[key].comment = resdata[key].comment==undefined?"":resdata[key].comment;
      }
      this.setData({EvaluateList: resdata});
      console.log(this.data.EvaluateList);
    })
  },
  // 滑动到底部时间
  scrolltolower: function(){
    wx.showLoading({title: '数据加载中'});
    this.getUserListText(this.data.sign);
  },
  // 时间戳转时间
  getLocalTime: function (t) {    
   var t = new Date(t), self = this;
   Date.prototype.toLocaleString = function() { 
    return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() + " " + self.mintime(this.getHours()) + ":" + self.mintime(this.getMinutes()) + ":" + self.mintime(this.getSeconds());
   };
   return t.toLocaleString();
  },
  // 处理时间小于10
  mintime: function(t){
    if(t < 10){return "0"+t};
    return t;
  },
  // 评价时间
  toast: function(content){
    wx.showToast({title: content,icon: 'none'});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})